var MTA_CONFIG = {
  app_id: "",
  event_id: "",
  api_base: "https://pingtas.qq.com/pingd",
  prefix: "_mta_",
  version: "1.3.3",
  stat_share_app: !1,
  stat_pull_down_fresh: !1,
  stat_reach_bottom: !1
};

function getNetworkType(a) {
  wx.getNetworkType({
    success: function (b) {
      a(b.networkType)
    }
  })
}

function getSystemInfo() {
  try {
    var a = wx.getSystemInfoSync();
    return {
      adt: encodeURIComponent(a.model),
      scl: a.pixelRatio,
      scr: a.windowWidth + "x" + a.windowHeight,
      lg: a.language,
      fl: a.version,
      jv: encodeURIComponent(a.system),
      tz: encodeURIComponent(a.platform)
    }
  }catch(err){}
}

function getUID() {
  try {
    return wx.getStorageSync(MTA_CONFIG.prefix + "auid")
  } catch (a) {}
}

function setUID() {
  try {
    var a = getRandom();
    wx.setStorageSync(MTA_CONFIG.prefix + "auid", a);
    return a
  } catch (b) {}
}

function getSID() {
  try {
    return wx.getStorageSync(MTA_CONFIG.prefix + "ssid")
  } catch (a) {}
}

function setSID() {
  try {
    var a = "s" + getRandom();
    wx.setStorageSync(MTA_CONFIG.prefix + "ssid", a);
    return a
  } catch (b) {}
}

function getRandom(a) {
  return (a || "") + Math.round(2147483647 * (Math.random() || .5)) * +new Date % 1E10
}

function getPagePath() {
  try {
    var a = getCurrentPages(),
      b = "/";
    0 < a.length && (b = a.pop().__route__);
    return b
  } catch (c) {
    console.log("get current page path error:" + c)
  }
}

function getMainInfo() {
  var a = {
    dm: "wechat.apps.xx",
    url: getPagePath(),
    pvi: "",
    si: "",
    ty: 0
  };
  a.pvi = function () {
    var b = getUID();
    b || (b = setUID(), a.ty = 1);
    return b
  }();
  a.si = function () {
    var a = getSID();
    a || (a = setSID());
    return a
  }();
  return a
}

function getBasicInfo() {
  try {
    var a = getSystemInfo();
    getNetworkType(function (a) {
      wx.setStorageSync(MTA_CONFIG.prefix + "ntdata", a)
    });
    a.ct = wx.getStorageSync(MTA_CONFIG.prefix + "ntdata") || "4g";
    return a
  } catch (err) {}
}

function getExtentInfo() {
  return {
    r2: MTA_CONFIG.app_id,
    r4: "wx",
    ext: "v=" + MTA_CONFIG.version + (null !== MTA.Data.userInfo ? ";ui=" + JSON.stringify(MTA.Data.userInfo) : "")
  }
}
var MTA = {
  App: {
    init: function (a) {
      "appID" in a && (MTA_CONFIG.app_id = a.appID);
      "eventID" in a && (MTA_CONFIG.event_id = a.eventID);
      "statShareApp" in a && (MTA_CONFIG.stat_share_app = a.statShareApp);
      "statPullDownFresh" in a && (MTA_CONFIG.stat_pull_down_fresh = a.statPullDownFresh);
      "statReachBottom" in a && (MTA_CONFIG.stat_reach_bottom = a.statReachBottom);
      setSID();
      "lauchOpts" in a && (MTA.Data.lanchInfo = a.lauchOpts, MTA.Data.lanchInfo.landing = 1)
    }
  },
  Page: {
    init: function () {
      var a = getCurrentPages()[getCurrentPages().length - 1];
      a.onShow &&
        ! function () {
          var b = a.onShow;
          a.onShow = function () {
            MTA.Page.stat();
            b.call(this, arguments)
          }
        }();
      MTA_CONFIG.stat_pull_down_fresh && a.onPullDownRefresh && ! function () {
        var b = a.onPullDownRefresh;
        a.onPullDownRefresh = function () {
          MTA.Event.stat(MTA_CONFIG.prefix + "pulldownfresh", {
            url: a.__route__
          });
          b.call(this, arguments)
        }
      }();
      MTA_CONFIG.stat_reach_bottom && a.onReachBottom && ! function () {
        var b = a.onReachBottom;
        a.onReachBottom = function () {
          MTA.Event.stat(MTA_CONFIG.prefix + "reachbottom", {
            url: a.__route__
          });
          b.call(this, arguments)
        }
      }();
      MTA_CONFIG.stat_share_app && a.onShareAppMessage && ! function () {
        var b = a.onShareAppMessage;
        a.onShareAppMessage = function () {
          MTA.Event.stat(MTA_CONFIG.prefix + "shareapp", {
            url: a.__route__
          });
          return b.call(this, arguments)
        }
      }()
    },
    stat: function () {
      if ("" != MTA_CONFIG.app_id) {
        var a = [],
          b = getExtentInfo(),
          c = [getMainInfo(), b, getBasicInfo()];
        MTA.Data.lanchInfo && (c.push({
            ht: MTA.Data.lanchInfo.scene,
            rdm: "/",
            rurl: MTA.Data.lanchInfo.path
          }), MTA.Data.lanchInfo.query && MTA.Data.lanchInfo.query._mta_ref_id && c.push({
            rarg: MTA.Data.lanchInfo.query._mta_ref_id
          }),
          MTA.Data.lanchInfo.landing && (b.ext += ";lp=1", delete MTA.Data.lanchInfo.landing));
        c.push({
          rand: +new Date
        });
        for (var b = 0, e = c.length; b < e; b++)
          for (var f in c[b]) c[b].hasOwnProperty(f) && a.push(f + "=" + ("undefined" == typeof c[b][f] ? "" : c[b][f]));
        wx.request({
          url: MTA_CONFIG.api_base + "?" + a.join("&").toLowerCase()
        })
      }
    }
  },
  Event: {
    stat: function (a, b) {
      if ("" != MTA_CONFIG.event_id) {
        var c = [],
          e = getMainInfo(),
          f = getExtentInfo();
        e.dm = "wxapps.click";
        e.url = a;
        f.r2 = MTA_CONFIG.event_id;
        var d;
        d = "undefined" === typeof b ? {} : b;
        var k = [],
          g;
        for (g in d) d.hasOwnProperty(g) && k.push(g + "=" + d[g]);
        d = k.join(";");
        f.r5 = d;
        d = 0;
        e = [e, f, getBasicInfo(), {
          rand: +new Date
        }];
        for (f = e.length; d < f; d++)
          for (var h in e[d]) e[d].hasOwnProperty(h) && c.push(h + "=" + ("undefined" == typeof e[d][h] ? "" : e[d][h]));
        wx.request({
          url: MTA_CONFIG.api_base + "?" + c.join("&").toLowerCase()
        })
      }
    }
  },
  Data: {
    userInfo: null,
    lanchInfo: null
  }
};
module.exports = MTA;